package io.renren.common.utils;

import io.renren.modules.video.entity.FileEntity;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MySpider {

    public static String getHtmlFromUrl(String url,String encoding){
        StringBuffer html = new StringBuffer();
        InputStreamReader isr=null;
        BufferedReader buf=null;
        String str = null;
        try {
            URL urlObj = new URL(url);
            URLConnection con = urlObj.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            isr = new InputStreamReader(con.getInputStream(),encoding);
            buf = new BufferedReader(isr);
            while((str=buf.readLine()) != null){
                html.append(str+"\n");
            }
            //sop(html.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(isr != null){
                try {
                    buf.close();
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return html.toString();
    }

    public static void  download(String uri) throws InterruptedException, IOException {
        // 记录开始下载的时间
        long begin_time = new Date().getTime();
        URL url=null;
        URLConnection conn=null;
        try{
            url= new URL(uri);

            // 获取连接
            conn= url.openConnection();
        }catch (IOException e){
            System.out.println("错误的url"+uri);
            return;
        }


        // 获取文件全路径
        String fileName = url.getFile();

        // 获取文件名
        fileName = fileName.substring(fileName.lastIndexOf("/"));

        System.out.println("开始下载>>>");

        // 获取文件大小
        int fileSize=0;
        try{
            fileSize = conn.getContentLength();
        }catch (Exception e){
            return;
        }


        System.out.println("文件总共大小：" + fileSize + "字节");

        // 设置分块大小
        int blockSize = 1024 * 1024;
        // 文件分块的数量
        int blockNum = fileSize / blockSize;

        if ((fileSize % blockSize) != 0) {
            blockNum += 1;
        }

        System.out.println("分块数->线程数：" + blockNum);

        Thread[] threads = new Thread[blockNum];
        for (int i = 0; i < blockNum; i++) {

            // 匿名函数对象需要用到的变量
            final int index = i;
            final int finalBlockNum = blockNum;
            final String finalFileName = fileName;

            // 创建一个线程
            URL finalUrl = url;
            int finalFileSize = fileSize;
            threads[i] = new Thread() {
                public void run() {
                    try {

                        // 重新获取连接
                        URLConnection conn = finalUrl.openConnection();
                        // 重新获取流
                        InputStream in = conn.getInputStream();
                        // 定义起始和结束点
                        int beginPoint = 0, endPoint = 0;

                        System.out.print("第" + (index + 1) + "块文件：");
                        beginPoint = index * blockSize;

                        // 判断结束点
                        if (index < finalBlockNum - 1) {
                            endPoint = beginPoint + blockSize;
                        } else {
                            endPoint = finalFileSize;
                        }

                        System.out.println("起始字节数：" + beginPoint + ",结束字节数：" + endPoint);

                        // 将下载的文件存储到一个文件夹中
                        //当该文件夹不存在时，则新建
                        File filePath = new File("D:\\temp/");
                        if (!filePath.exists()) {
                            filePath.mkdirs();
                        }

                        FileOutputStream fos = new FileOutputStream(new File("D:\\temp/", finalFileName + "_" + (index + 1)));

                        // 跳过 beginPoint个字节进行读取
                        in.skip(beginPoint);
                        byte[] buffer = new byte[1024];
                        int count;
                        // 定义当前下载进度
                        int process = beginPoint;
                        // 当前进度必须小于结束字节数
                        while (process < endPoint) {

                            count = in.read(buffer);
                            // 判断是否读到最后一块
                            if (process + count >= endPoint) {
                                count = endPoint - process;
                                process = endPoint;
                            } else {
                                // 计算当前进度
                                process += count;
                            }
                            // 保存文件流
                            fos.write(buffer, 0, count);

                        }
                        fos.close();
                        in.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            };
            threads[i].start();

        }

        // 当所有线程都结束时才开始文件的合并
        for (Thread t : threads) {
            t.join();
        }

        // 若该文件夹不存在，则创建一个文件夹
        File filePath = new File("D:/temp/download/");
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        // 定义文件输出流
        FileOutputStream fos = new FileOutputStream("D:/temp/download/" + fileName);
        for (int i = 0; i < blockNum; i++) {
            FileInputStream fis = new FileInputStream("D:\\temp/" + fileName + "_" + (i + 1));
            byte[] buffer = new byte[1024];
            int count;
            while ((count = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            fis.close();
        }
        fos.close();

        long end_time = new Date().getTime();
        long seconds = (end_time - begin_time) / 1000;
        long minutes = seconds / 60;
        long second = seconds % 60;

        System.out.println("下载完成,用时：" + minutes + "分" + second + "秒");


    }

    public static List<FileEntity> getFileEntiys(String url) throws IOException {
        Document document= Jsoup.parse(new URL(url),1000);
        //  Document document= Jsoup.parse(new URL("https://cn.bing.com/images/search?q=%e4%b8%8a%e6%b5%b7%e7%ac%ac%e4%b8%80%e6%89%b9%e4%b8%89%e5%8c%ba%e5%90%8d%e5%8d%95%e5%b7%b2%e7%bb%8f%e5%88%92%e5%ae%9a&form=ISTRTH&id=81D0AEF88B4D909C9001AC1E8D80D49BC6A713B1&cat=%E4%BB%8A%E6%97%A5%E7%83%AD%E5%9B%BE&disoverlay=1&first=1&tsc=ImageHoverTitle/"),1000);
        Elements images = document.select("img");
//        String http="https:";
        String http="";
        System.out.println("文件数量:"+images.size()+"-----------------");
        List<FileEntity> fileEntities=new ArrayList<>(images.size());
        for (Element image : images)
        {
            FileEntity entity=new FileEntity();
//            System.out.println("src : " + image.attr("src"));
//            System.out.println("height : " + image.attr("height"));
//            System.out.println("width : " + image.attr("width"));
//            System.out.println("alt : " + image.attr("alt"));
            String src = image.attr("src");
            if(StringUtils.isBlank(src)){
                continue;
            }
            src=http+src;
            entity.setPath(src);
            fileEntities.add(entity);
        }
        return fileEntities;
    }



    public static void main(String[] args) throws IOException, InterruptedException {
//        //System.out.println(getHtmlFromUrl("https://www.bilibili.com/","utf-8"));
        Document document= Jsoup.parse(new URL("https://www.bilibili.com/"),1000);
      //  Document document= Jsoup.parse(new URL("https://cn.bing.com/images/search?q=%e4%b8%8a%e6%b5%b7%e7%ac%ac%e4%b8%80%e6%89%b9%e4%b8%89%e5%8c%ba%e5%90%8d%e5%8d%95%e5%b7%b2%e7%bb%8f%e5%88%92%e5%ae%9a&form=ISTRTH&id=81D0AEF88B4D909C9001AC1E8D80D49BC6A713B1&cat=%E4%BB%8A%E6%97%A5%E7%83%AD%E5%9B%BE&disoverlay=1&first=1&tsc=ImageHoverTitle/"),1000);
      Elements images = document.select("img");
      String http="http:";
        System.out.println("文件数量:"+images.size()+"-----------------");
        for (Element image : images)
        {
//            System.out.println("src : " + image.attr("src"));
//            System.out.println("height : " + image.attr("height"));
//            System.out.println("width : " + image.attr("width"));
//            System.out.println("alt : " + image.attr("alt"));
            String src = image.attr("src");
            if(StringUtils.isBlank(src)){
                continue;
            }
            src=http+src;

            download(src);

        }




    }
}
